﻿using Jamesnet.Wpf.Animation;
using Jamesnet.Wpf.Controls;
using MaterialDesignColors.ColorManipulation;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace Central_Control.展厅
{
    /// <summary>
    /// Gallery.xaml 的交互逻辑
    /// </summary>
    /// 
    public partial class Gallery : Window
    {
        ValueItem _vi;
        Storyboard _sb;

        Border CurrentPage;
        public GalleryButtonInfo galleryButtonInfo;
        PCAdmini pCAdmini;
        public Gallery()
        {
            InitializeComponent();

            Loaded += MainWindow_Loaded;
        }
        //    private Button lastClickedButton;

        //    private void BottomButton_Click(object sender, RoutedEventArgs e)
        //    {
        //        Button clickedButton = (Button)sender;

        //        // 如果存在上一个点击的按钮，将其样式还原
        //        if (lastClickedButton != null)
        //        {
        //            RestoreButtonStyle(lastClickedButton);
        //        }

        //        // 将当前点击的按钮样式更改为绿色
        //        ChangeButtonStyle(clickedButton);

        //        // 更新上一个点击的按钮
        //        lastClickedButton = clickedButton;
        //    }

        //    private void ChangeButtonStyle(Button button)
        //    {
        //        //button.Background = new SolidColorBrush(Colors.Green);
        //        //button.BorderBrush = new SolidColorBrush(Colors.Green);
        //        // 创建颜色动画（Background）
        //        ColorAnimation backgroundAnimation = new ColorAnimation
        //        {
        //            To = Colors.Green,
        //            Duration = TimeSpan.FromSeconds(0.5)
        //        };

        //        // 创建颜色动画（BorderBrush）
        //        ColorAnimation borderBrushAnimation = new ColorAnimation
        //        {
        //            To = Colors.Green,
        //            Duration = TimeSpan.FromSeconds(0.5)
        //        };

        //        // 创建故事板
        //        Storyboard storyboard = new Storyboard();
        //        storyboard.Children.Add(backgroundAnimation);
        //        storyboard.Children.Add(borderBrushAnimation);

        //        // 将动画应用于按钮的Background和BorderBrush
        //        Storyboard.SetTarget(backgroundAnimation, button);
        //        Storyboard.SetTargetProperty(backgroundAnimation, new PropertyPath("(Button.Background).(SolidColorBrush.Color)"));

        //        Storyboard.SetTarget(borderBrushAnimation, button);
        //        Storyboard.SetTargetProperty(borderBrushAnimation, new PropertyPath("(Button.BorderBrush).(SolidColorBrush.Color)"));

        //        // 启动动画
        //        storyboard.Begin(this);
        //    }

        //    private void RestoreButtonStyle(Button button)
        //    {
        //        //button.Background = new SolidColorBrush(Colors.Gray);
        //        //button.BorderBrush = new SolidColorBrush(Colors.Gray);

        //        // 创建颜色动画（Background）
        //        ColorAnimation backgroundAnimation = new ColorAnimation
        //        {
        //            To = Colors.Gray,
        //            Duration = TimeSpan.FromSeconds(0.5)
        //        };

        //        // 创建颜色动画（BorderBrush）
        //        ColorAnimation borderBrushAnimation = new ColorAnimation
        //        {
        //            To = Colors.Gray,
        //            Duration = TimeSpan.FromSeconds(0.5)
        //        };

        //        // 创建故事板
        //        Storyboard storyboard = new Storyboard();
        //        storyboard.Children.Add(backgroundAnimation);
        //        storyboard.Children.Add(borderBrushAnimation);

        //        // 将动画应用于按钮的Background和BorderBrush
        //        Storyboard.SetTarget(backgroundAnimation, button);
        //        Storyboard.SetTargetProperty(backgroundAnimation, new PropertyPath("(Button.Background).(SolidColorBrush.Color)"));

        //        Storyboard.SetTarget(borderBrushAnimation, button);
        //        Storyboard.SetTargetProperty(borderBrushAnimation, new PropertyPath("(Button.BorderBrush).(SolidColorBrush.Color)"));

        //        // 启动动画
        //        storyboard.Begin(this);

        //    }

        //    private void BottomButton_MouseEnter(object sender, MouseEventArgs e)
        //    {
        //        Button clickedButton = (Button)sender;


        //        clickedButton.SetValue(ShadowAssist.ShadowDepthProperty, ShadowDepth.Depth3);
        //    }

        //    private void BottomButton_MouseLeave(object sender, MouseEventArgs e)
        //    {
        //        Button clickedButton = (Button)sender;
        //        clickedButton.SetValue(ShadowAssist.ShadowDepthProperty, ShadowDepth.Depth1);
        //    }
        //}

        #region 导航栏
        private void InitStoryBoard(Grid circle)
        {
            _vi = new();
            _sb = new();

            _vi.Mode = EasingFunctionBaseMode.QuinticEaseInOut;
            _vi.Property = new PropertyPath(Canvas.LeftProperty);
            _vi.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 500));

            Storyboard.SetTarget(_vi, circle);
            Storyboard.SetTargetProperty(_vi, _vi.Property);

            _sb.Children.Add(_vi);
        }
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // 找到 ListBox 控件的模板
            ControlTemplate listBoxTemplate = NavigationBar.Template;

            // 在模板中查找 Part_Circle 元素
            Grid partCircle = (Grid)listBoxTemplate.FindName("Part_Circle", NavigationBar);
            InitStoryBoard(partCircle);
            NavigationBar.SelectionChanged += ListBox_SelectionChanged;
            NavigationBar.SelectedIndex = 0;
        }
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _vi.To = NavigationBar.SelectedIndex * 80;
            _sb.Begin();
            // 在这里处理选中项更改的逻辑
            if (NavigationBar.SelectedItem != null)
            {
                var selectedContent = (NavigationBar.SelectedItem as ListBoxItem)?.Content;
                if (selectedContent != null)
                {
                    // 执行你的逻辑，例如：
                    // var content = ((ListBoxItem)selectedContent).Content;
                    // MessageBox.Show($"Selected: {content}");
                }



                if (NavigationBar.SelectedIndex == 0)
                {
                    PageAnima(page1);
                    CurrentPage = page1;
                }
                else if (NavigationBar.SelectedIndex == 1)
                {
                    InitPC();
                    PageAnima(page2);
                    CurrentPage = page2;
                }
                else if (NavigationBar.SelectedIndex == 2)
                {
                    PageAnima(page3);
                    CurrentPage = page3;
                }
                else if (NavigationBar.SelectedIndex == 3)
                {
                    PageAnima(page4);
                    CurrentPage = page4;
                }
                else if (NavigationBar.SelectedIndex == 4)
                {
                    PageAnima(page5);
                    CurrentPage = page5;
                }
                else if (NavigationBar.SelectedIndex == 5)
                {
                    PageAnima(page6);
                    CurrentPage = page6;
                }
            }
        }
        private void PageAnima(Border Target)
        {

            if (CurrentPage != null)
            {
                // 创建动画
                Storyboard storyboard = new Storyboard();

                // page1 上移和淡入
                DoubleAnimation MoveInto = new DoubleAnimation
                {
                    To = 0,
                    Duration = TimeSpan.FromSeconds(0.5)
                };
                Storyboard.SetTarget(MoveInto, Target);
                Storyboard.SetTargetProperty(MoveInto, new PropertyPath("(RenderTransform).(TranslateTransform.X)"));

                DoubleAnimation PageCover = new DoubleAnimation
                {
                    To = 1.0,
                    Duration = TimeSpan.FromSeconds(0.5)
                };
                Storyboard.SetTarget(PageCover, Target);
                Storyboard.SetTargetProperty(PageCover, new PropertyPath("Opacity"));

                // page2 下移和淡出
                DoubleAnimation MoveOut = new DoubleAnimation
                {
                    To = ActualWidth,
                    Duration = TimeSpan.FromSeconds(0.3)
                };
                Storyboard.SetTarget(MoveOut, CurrentPage);
                Storyboard.SetTargetProperty(MoveOut, new PropertyPath("(RenderTransform).(TranslateTransform.X)"));

                DoubleAnimation PageHide = new DoubleAnimation
                {
                    To = 0,
                    Duration = TimeSpan.FromSeconds(0.5)
                };
                Storyboard.SetTarget(PageHide, CurrentPage);
                Storyboard.SetTargetProperty(PageHide, new PropertyPath("Opacity"));

                // 将动画添加到Storyboard
                storyboard.Children.Add(MoveInto);
                storyboard.Children.Add(MoveOut);
                storyboard.Children.Add(PageCover);
                storyboard.Children.Add(PageHide);

                // 启动动画
                storyboard.Begin();
            }
            else
            {
                // 创建动画
                Storyboard storyboard = new Storyboard();

                // page1 上移和淡入
                DoubleAnimation MoveInto = new DoubleAnimation
                {
                    To = 0,
                    Duration = TimeSpan.FromSeconds(0.5)
                };
                Storyboard.SetTarget(MoveInto, Target);
                Storyboard.SetTargetProperty(MoveInto, new PropertyPath("(RenderTransform).(TranslateTransform.X)"));

                DoubleAnimation PageCover = new DoubleAnimation
                {
                    To = 1.0,
                    Duration = TimeSpan.FromSeconds(0.5)
                };
                Storyboard.SetTarget(PageCover, Target);
                Storyboard.SetTargetProperty(PageCover, new PropertyPath("Opacity"));

                storyboard.Children.Add(MoveInto);
                storyboard.Children.Add(PageCover);

                // 启动动画
                storyboard.Begin();
            }

        }
        private void ReturnButton_Click(object sender, RoutedEventArgs e)
        {


            MainWindow mainWindow = new MainWindow();

            mainWindow.Visibility = Visibility.Visible;

            mainWindow.Left = this.Left;
            mainWindow.Top = this.Top;
            this.Close();
        }
        private void FindButton_Click(object sender, RoutedEventArgs e)
        {
            AddPC addPC = new AddPC(galleryButtonInfo, this);

            addPC.Owner = this; // 设置新窗口的Owner为当前窗口
            addPC.WindowStartupLocation = WindowStartupLocation.CenterOwner; // 设置新窗口在Owner窗口中间打开
            addPC.ShowDialog();
        }
        #endregion

        #region 首页
        private void AwakeUpAllPcButton_Click(object sender, RoutedEventArgs e)
        {
            if (pCAdmini == null)
            {
                //pCAdmini = new PCAdmini(this);
            }
            if (galleryButtonInfo.AddPcInfos != null && galleryButtonInfo.AddPcInfos.Count > 0)
            {
                for (int i = 0;i<galleryButtonInfo.AddPcInfos.Count;i++)
                {
                    pCAdmini.WakeUpAsync(galleryButtonInfo.AddPcInfos[i].IP, galleryButtonInfo.AddPcInfos[i].MAC);
                }
            }
        }

        private void ShutDownAllPcButton_Click(object sender, RoutedEventArgs e)
        {
            if (pCAdmini == null)
            {
                //pCAdmini = new PCAdmini(this);
            }
            if (galleryButtonInfo.AddPcInfos != null && galleryButtonInfo.AddPcInfos.Count > 0)
            {
                for (int i = 0; i < galleryButtonInfo.AddPcInfos.Count; i++)
                {
                    pCAdmini.SendMessageToComputer(galleryButtonInfo.AddPcInfos[i].IP, "关机：5");
                }
            }
        }
        #endregion

        #region 电脑页面
        public void InitPC()
        {
            if (pCAdmini == null)
            {
                //pCAdmini = new PCAdmini(this);
            }
            pCAdmini.Init(PCs);
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button && button.DataContext is PcInfo pcInfo)
            {
                // 弹出对话框，让用户输入要删除的按钮名称
                MessageBoxResult result = MessageBox.Show($"要删除{pcInfo.IP}吗?", "提示", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    PcInfo targetButton = galleryButtonInfo.AddPcInfos.FirstOrDefault(button => button.ID == pcInfo.ID);
                    if (targetButton != null)
                    {
                        galleryButtonInfo.AddPcInfos.Remove(targetButton);
                        PcInfo targetpcinfo = galleryButtonInfo.pcInfos.FirstOrDefault(button => button.IP == pcInfo.IP);
                        if (targetpcinfo != null)
                        {
                            targetpcinfo.isAdd = false;
                        }
                        GalleryButtonInfo result2 = MainWindow.instance.Buttons.FirstOrDefault(button => button.ID == galleryButtonInfo.ID);
                        if (result != null)
                        {
                            result2.AddPcInfos = galleryButtonInfo.AddPcInfos;
                            result2.pcInfos = galleryButtonInfo.pcInfos;
                            MainWindow.instance.SaveGalleryButtonInfoToJson();
                        }
                        InitPC();
                    }
                }
            }

        }

        private void AwakeUpButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button && button.DataContext is PcInfo pcInfo)
            {
                // 弹出对话框，让用户输入要删除的按钮名称
                MessageBoxResult result = MessageBox.Show($"要唤醒{pcInfo.IP}吗?", "提示", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    pCAdmini.WakeUpAsync(pcInfo.IP, pcInfo.MAC);
                }
            }
        }

        private void ShutDownButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button && button.DataContext is PcInfo pcInfo)
            {
                // 弹出对话框，让用户输入要删除的按钮名称
                MessageBoxResult result = MessageBox.Show($"要关闭{pcInfo.IP}吗?", "提示", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    pCAdmini.SendMessageToComputer(pcInfo.IP, "关机：5");
                }
            }
        }


        #endregion
    }
}
